package gg.com.toolbox.activity;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;

import gg.com.toolbox.R;
import gg.com.toolbox.adapter.NotepadAdapter;
import gg.com.toolbox.bean.SecrecyBean;
import gg.com.toolbox.sqlite.DBManager;
import gg.com.toolbox.tools.BaseActivity;
import gg.com.toolbox.util.AppConstant;
import gg.com.toolbox.util.DateUtils;
import gg.com.toolbox.util.MyApplication;
import gg.com.toolbox.view.MyAlertDialog;

/**
 * 记录功能页面
 * Created by gaoge on 2017/10/10.
 */

public class NotepadActivity extends BaseActivity implements View.OnClickListener {
    //标题
    private TextView title = null;
    //返回键
    private ImageView img_left = null;
    //添加信息
    private ImageView img_right = null;
    //列表数据显示
    private ListView listview = null;
    //适配器
    private NotepadAdapter notepadAdapter = null;
    //保存数据的集合
    private ArrayList<SecrecyBean> list = new ArrayList<>();
    //标题栏点击次数
    private long mExitTime = 0;
    //加载框
    private MyAlertDialog mydialog;
    //数据库操作类
    private DBManager db = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //加载布局
        setContentView(R.layout.notepad_activity);
        //将页面添加进控制器
        MyApplication.getInstance().addActivity(this);
        //初始化组件
        init();
        //功能实现
        realize();
    }

    /**
     * 初始化组件
     */
    private void init() {
        db = new DBManager(this);
        listview = (ListView) findViewById(R.id.listview);
        title = (TextView) findViewById(R.id.title);
        img_left = (ImageView) findViewById(R.id.img_left);
        img_right = (ImageView) findViewById(R.id.img_right);
        img_right.setVisibility(View.VISIBLE);
    }

    /**
     * 功能实现
     */
    private void realize() {
        //设置标题
        title.setText("账号信息");
        //添加数据显示适配器
        notepadAdapter = new NotepadAdapter(this, list);
        listview.setAdapter(notepadAdapter);
        list = db.selectUserName();
        notepadAdapter.setData(list);
        //添加点击事件
        img_left.setOnClickListener(this);
        img_right.setOnClickListener(this);
        title.setOnClickListener(this);
        //listview长按事件
        listview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
                mydialog = new MyAlertDialog();
                mydialog.deleteUserNameDialog(NotepadActivity.this, R.layout.delete_dialog);
                TextView cancel_text = mydialog.getTextView1();
                TextView confirm_text = mydialog.getTextView2();
                //取消
                cancel_text.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mydialog.dismissAd();
                    }
                });
                //删除
                confirm_text.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        db.deleteUserNameItem(list.get(position).getId());
                        list = db.selectUserName();
                        notepadAdapter.setData(list);
                        mydialog.dismissAd();
                    }
                });
                closeKeyboard();
                return true;
            }
        });
        //listview点击事件
        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                mydialog = new MyAlertDialog();
                mydialog.addUserNameDialog(NotepadActivity.this, R.layout.add_username_dialog, 1);
                final EditText title_edit = mydialog.getEditText1();
                final EditText username_edit = mydialog.getEditText2();
                final EditText password_edit = mydialog.getEditText3();
                final EditText remarks_edit = mydialog.getEditText4();
                title_edit.setText(list.get(position).getTitle());
                title_edit.setEnabled(false);
                title_edit.setFocusable(false);
                username_edit.setText(list.get(position).getUserName());
                username_edit.setSelection(list.get(position).getUserName().length());
                password_edit.setText(list.get(position).getPassWord());
                password_edit.setSelection(list.get(position).getPassWord().length());
                remarks_edit.setText(list.get(position).getRemarks());
                remarks_edit.setSelection(list.get(position).getRemarks().length());
                TextView cancel_text = mydialog.getTextView1();
                TextView confirm_text = mydialog.getTextView2();
                confirm_text.setText("修改");
                //取消
                cancel_text.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mydialog.dismissAd();
                    }
                });
                //修改
                confirm_text.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (!title_edit.getText().toString().equals("") &&
                                !username_edit.getText().toString().equals("") &&
                                !password_edit.getText().toString().equals("")) {
                            db.updateUserName(title_edit.getText().toString(), username_edit.getText().toString(),
                                    password_edit.getText().toString(), remarks_edit.getText().toString());
                            list = db.selectUserName();
                            notepadAdapter.setData(list);
                            mydialog.dismissAd();
                        } else {
                            Toast.makeText(NotepadActivity.this, "信息不能为空", 1000).show();
                        }
                    }
                });
                closeKeyboard();
            }
        });
    }

    /**
     * 点击事件
     *
     * @param v
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //返回键
            case R.id.img_left:
                finish();
                break;
            //添加键
            case R.id.img_right:
                //顶部布局组件
//                View inflate = LayoutInflater.from(this).inflate(R.layout.add_username_dialog, null);
//                listview.addHeaderView(inflate);
                //加载弹出框
                mydialog = new MyAlertDialog();
                mydialog.addUserNameDialog(this, R.layout.add_username_dialog, 0);
                final EditText title_edit = mydialog.getEditText1();
                final EditText username_edit = mydialog.getEditText2();
                final EditText password_edit = mydialog.getEditText3();
                final EditText remarks_edit = mydialog.getEditText4();
                TextView cancel_text = mydialog.getTextView1();
                TextView confirm_text = mydialog.getTextView2();
                //取消
                cancel_text.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mydialog.dismissAd();
                    }
                });
                //添加
                confirm_text.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (!title_edit.getText().toString().equals("") &&
                                !username_edit.getText().toString().equals("") &&
                                !password_edit.getText().toString().equals("")) {
                            db.insertUserName(title_edit.getText().toString(), username_edit.getText().toString(),
                                    password_edit.getText().toString(), remarks_edit.getText().toString());
                            list = db.selectUserName();
                            notepadAdapter.setData(list);
                            mydialog.dismissAd();
                        } else {
                            Toast.makeText(NotepadActivity.this, "信息不能为空", 1000).show();
                        }
                    }
                });
                closeKeyboard();
                break;
            //标题栏
            case R.id.title:
                //如果是双击
                if ((System.currentTimeMillis() - mExitTime) > 1000) {
                    mExitTime = System.currentTimeMillis();
                } else {
                    listview.smoothScrollToPosition(0);
                }
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        AppConstant.time = DateUtils.getDateLong_yMdHms();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //将页面从控制器中移除
        MyApplication.getInstance().deleteActivity(this);
    }

    /**
     * 关闭键盘
     */
    public void closeKeyboard() {
        InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        if(imm.isActive()&&getCurrentFocus()!=null){
            if (getCurrentFocus().getWindowToken()!=null) {
                imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }
    }
}
